/**
 * Created by webrx on 2024/4/24.
 */
import type {App, AsyncComponentLoader} from 'vue'
import {defineAsyncComponent} from 'vue'

const VueAutoRegister = {
    install: (app: App) => {
        // import.meta.glob是vite的新api  /@/components/**.vue  @/**/**.vue
        const components: any = import.meta.glob("@/components/user/**.vue")
        // 遍历组件模块实现自动注册
        for (const [path, component] of Object.entries(components)) {
            // 把文件名称当作组件注册的 name
            const componentName = path.slice(path.lastIndexOf("/") + 1, path.lastIndexOf("."))
            // 通过 defineAsyncComponent 异步导入指定路径下的组件
            app.component(componentName, defineAsyncComponent(component as AsyncComponentLoader))
        }
    }
}
export default VueAutoRegister
